
   class Register{
    constructor(){
        this.username = document.querySelector('#recipient-name')
        this.phone = document.querySelector('#recipient-phone')
        this.password = document.querySelector('#recipient-password')
        this.checkpassword = document.querySelector('#recipient-checkpassword')
        this.btn = document.querySelector('button')
        this.sp1 = document.querySelector('.sp1')
        this.sp2 = document.querySelector('.sp2')
        this.a = document.querySelector('a')
        this.showname = false
        this.showphone = false
        this.init()
    }
    init(){
        this.handleRegister()
        this.checkUrsename()
        this.Checkphone()
        this.click()
    }
    click(){
        this.a.onclick =()=>{
          location.href = 'Sign.html'
        }
      }
      //点击登录按钮判断信息是否填写完整 已经两次密码是否输入一致
      handleRegister() {
        this.btn.onclick = () => {
          if (this.username.value==''||this.phone.value==''||this.password.value==''||this.checkpassword.value=='') {
            alert('请输入完整的信息')
            return false
          }
          if (this.password.value != this.checkpassword.value) {
            alert('密码输入不一致')
            return
          }

          //创建一个对象，保存所有填写的信息
          var obj = {
            username: this.username.value,
            phone: this.phone.value,
            password: this.password.value,
            checkpassword: this.checkpassword.value
          }
          console.log(this.showname,this.showphone)
          if(!this.showname && !this.showphone){
            var arr = this.getRegisterInif()
            arr.push(obj)
            localStorage.setItem('inif', JSON.stringify(arr))
            this.username.value = ''
            this.phone.value = ''
            this.password.value = ''
            this.checkpassword.value = ''
            alert('注册成功')
            setTimeout(()=>{
              location.href='Sign.html'
            },1000)
          }else{
            alert('注册信息有误！')
            return
          }
         
        }
        //
      }
      getRegisterInif() {
        return JSON.parse(localStorage.getItem('inif')) || []
      }

      //判断用户名是否已被注册
      checkUrsename() {
        //给用户名的输入框绑定事件（失去焦点）
        this.username.onblur = () => {
          //获取注册列表的值
          let arr = this.getRegisterInif()
          //当注册列表里面有值的时候，判断输入的值是否在列表中存在
          if (arr.length > 0) {
            var res = arr.some(item => { return item.username == this.username.value });
            this.showname = res
          }
          if (res) {
            this.sp1.innerHTML = '该用户名已存在'
          } else {
            this.sp1.innerHTML = ''
          }
        }
      }

      //验证手机号
      Checkphone(){
        this.phone.onblur = ()=>{
          //获取注册列表的值
          let arr = this.getRegisterInif()
          if(arr.length > 0){
            var res = arr.some((res)=>{
              return res.phone == this.phone.value
            });
            this.showphone = res
          }
          if(res){
            this.sp2.innerHTML = '该手机号已被注册'
          }else{
            this.sp2.innerHTML = ''
          }
        }
      }
   }
   new Register()
   
